; Perspective Reflection quirli
; Version 0.10
; By David Nickerson
; Copyright 2009 David Nickerson
; Modified by Marcel Suter

; Description:
; This script modifies a layer to have a perspective and reflection consistent with that perspective.
; Updated to Version 0.20
; Updates provided by "Hoffi" via the Registry at http://registry.gimp.org/comment/reply/15784

; License:
; This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
; This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
; The GNU Public License is available at <http://www.gnu.org/licenses/>.


(define (perspective-reflection-quirli img layer1 rotate dist c-height)

	; Begin the 'undo' group
	(gimp-image-undo-group-start img)
	(gimp-context-push)

	; Create a layer and define some values
	(let* (
		(layer2 (car (gimp-layer-copy layer1 1)))
		(mask (car (gimp-layer-create-mask layer2 ADD-ALPHA-MASK)))
        (gradient (car (gimp-gradient-new "Gray to Black for fade-out")))
		(old-width (car (gimp-drawable-width layer1)))
		(old-height (car (gimp-drawable-height layer1)))
		(new-width (* (cos (* rotate 0.0174532925)) old-width))
		(new-height (* old-height (- 1 (* (sin (* rotate 0.0174532925)) (- 1 (/ dist 100))))))
		)


	; Add the layer
	(gimp-image-add-layer img layer2 1)
	(gimp-layer-add-mask layer2 mask)
	
	; Initialize the gradient
    (gimp-gradient-segment-set-left-color gradient 0 '(200 200 200) 100)
    (gimp-gradient-segment-set-right-color gradient 0 '(0 0 0) 100)
    ; this goes to black very fast...
    (gimp-gradient-segment-range-set-blending-function gradient 0 -1 GRADIENT-SEGMENT-SPHERE-INCREASING)
    ; ...so we could move the middle pos to fade-out not soo fast
    (gimp-gradient-segment-set-middle-pos gradient 0 0.5)
    ; finally make it active
    (gimp-context-set-gradient gradient)

    ;now draw the gradient to the layer mask:
    (gimp-layer-set-edit-mask layer2 TRUE)
    (gimp-edit-blend mask CUSTOM-MODE NORMAL-MODE GRADIENT-LINEAR 100 0 REPEAT-NONE FALSE FALSE 1 0 FALSE 0 old-height 0 (/ (* old-height 1) 3))
    (gimp-layer-set-edit-mask layer2 FALSE) 

    ;Apply the mask
    (gimp-layer-remove-mask layer2 MASK-APPLY)	

	; Perform the transformations
	(gimp-drawable-transform-perspective layer1
		0 0
		new-width (* (- old-height new-height) (- 1 (/ c-height 100)))
		0 old-height
		new-width (+ (* (- old-height new-height) (- 1 (/ c-height 100))) new-height)
		0 2 0 3 0)

	(gimp-drawable-transform-perspective layer2
		0 (* old-height 2)
		new-width (+ (* (- old-height new-height) (- 1 (/ c-height 100))) (* new-height 2))
		0 old-height
		new-width (+ (* (- old-height new-height) (- 1 (/ c-height 100))) new-height)
		0 2 0 3 0)
		
	;Delete the gradient
	(gimp-gradient-delete gradient)

	; Resize the canvas
	(gimp-image-resize-to-layers img)

	; End the 'undo' group
	(gimp-context-pop)
	(gimp-image-undo-group-end img)

	; Display the changes
	(gimp-displays-flush)

))

(script-fu-register "perspective-reflection-quirli"
					"Perspective Reflection quirli"
					"Modifies a layer to have a perspective and dim reflection"
					"David Nickerson"
					"David Nickerson"
					"2009"
					"RGB*, GRAY*"
					SF-IMAGE	"Image"						0
					SF-DRAWABLE	"Layer"						0
					SF-VALUE	"Rotation (-90 to 90)"		"25"
					SF-VALUE	"Distance (0 to 100)"		"70"
					SF-VALUE	"Camera height (0=bottom, 100=top)"	"50")

(script-fu-menu-register "perspective-reflection-quirli"
						 "<Image>/Filters/Decor")